package com.example.demo.controller;

import com.example.demo.constant.Constants;
import com.example.demo.model.BlogInfo;
import com.example.demo.model.Result;
import com.example.demo.model.UserInfo;
import com.example.demo.service.BlogInfoService;
import com.example.demo.service.UserInfoService;
import com.example.demo.utils.JwtUtils;
import com.example.demo.utils.SecurityUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private BlogInfoService blogInfoService;

    @RequestMapping("/login")
    public Result<String> login(String userName, String password){
        log.info("/user/login: userName: {}, password: {}", userName, password);
        // 1. 校验参数是否为空
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return Result.fail("用户名或密码为空");
        }
        // 2. 校验用户是否存在
        UserInfo userInfo = userInfoService.selectByName(userName);
        if(userInfo == null){
            return Result.fail("用户不存在");
        }
        // 3. 校验密码是否正确
        String sqlPassword = userInfo.getPassword();
        if(!SecurityUtils.verify(password, sqlPassword)){
            log.error("/user/login, 密码错误");
            return Result.fail("密码错误");
        }
        // 4. 校验成功，发方令牌
        userInfo.setPassword("");

        Map<String, Object> claim = new HashMap<>();
        claim.put(Constants.USER_ID, userInfo.getId());
        claim.put(Constants.USER_NAME, userInfo.getUserName());

        String token = JwtUtils.genJwtToken(claim);
        return Result.success(token);
    }

    @RequestMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        String token = request.getHeader(Constants.USER_INFO_TOKEN);
        Claims claims = JwtUtils.parseToken(token);
        Integer userId = (Integer) claims.get(Constants.USER_ID);

        UserInfo userInfo = userInfoService.selectById(userId);
        if(userInfo == null){
            log.error("getUserInfo， 用户不存在！");
            return Result.fail("用户不存在");
        }
        return Result.success(userInfo);
    }

    @RequestMapping("/getAuthorInfo")
    public Result getAuthorInfo(Integer blogId){
        log.info("getAuthorInfo, 接收到参数 blogId: {}", blogId);
        if(blogId == null){
            log.error("getAuthorInfo， 参数输入不合法！");
            return Result.fail("参数输入不合法");
        }
        BlogInfo blogInfo = blogInfoService.getBlogById(blogId);
        if(blogInfo == null){
            log.error("getAuthorInfo, 博客不存在");
            return Result.fail("博客不存在");
        }
        Integer userId = blogInfo.getUserId();
        UserInfo userInfo = userInfoService.selectById(userId);
        if(userInfo == null){
            log.error("getAuthorInfo, 作者不存在");
            return Result.fail("作者不存在");
        }
        return Result.success(userInfo);
    }
}
